﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using System.Data;
using Maticsoft.Common;
using System.Drawing;

namespace Admin.Finance
{
    public partial class SubInvoicePayMentList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        #region gridView
        public void BindData()
        {
            #region
            //if (!Context.User.Identity.IsAuthenticated)
            //{
            //    return;
            //}
            //AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
            //if (user.HasPermissionID(PermId_Modify))
            //{
            //    gridView.Columns[6].Visible = true;
            //}
            //if (user.HasPermissionID(PermId_Delete))
            //{
            //    gridView.Columns[7].Visible = true;
            //}
            #endregion
            StringBuilder strWhere = new StringBuilder();
            if (txtMinInvoiceNumber.Text.Trim() != "" && txtMaxInvoiceNumber.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and InvoiceNumber between '{0}' and '{1}'", txtMinInvoiceNumber.Text.Trim(), txtMaxInvoiceNumber.Text.Trim());
            }
            else if (txtMinInvoiceNumber.Text.Trim() != "" && txtMaxInvoiceNumber.Text.Trim() == "")
            {
                strWhere.AppendFormat(" and InvoiceNumber>'{0}'", txtMinInvoiceNumber.Text.Trim());
            }
            else if (txtMinInvoiceNumber.Text.Trim() == "" && txtMaxInvoiceNumber.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and InvoiceNumber<'{0}'", txtMaxInvoiceNumber.Text.Trim());
            }
            if (txtCustomer.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and Customer like '%{0}%'", txtCustomer.Text.Trim());
            }
            if (txtCreateUser.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and CreateUser like '%{0}%'", txtCreateUser.Text.Trim());
            }
            if (txtInvoiceReceiveMan.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and ReceiveMan like '%{0}%'", txtInvoiceReceiveMan.Text.Trim());
            }
            if (txtInvoiceBeginDate.Value != "" && txtInvoiceEndDate.Value != "")
            {
                strWhere.AppendFormat(" and InvoiceDate between '{0}' and '{1}'", txtInvoiceBeginDate.Value, txtInvoiceEndDate.Value);
            }
            else if (txtInvoiceBeginDate.Value != "" && txtInvoiceEndDate.Value == "")
            {
                strWhere.AppendFormat(" and InvoiceDate>'" + txtInvoiceBeginDate.Value + "'");
            }
            else if (txtInvoiceBeginDate.Value == "" && txtInvoiceEndDate.Value != "")
            {
                strWhere.AppendFormat(" and InvoiceDate<'" + txtInvoiceEndDate.Value + "'");
            }
            if (txtDepartMent.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and CreateDepartment like '%{0}%'", txtDepartMent.Text.Trim());
            }
            try
            {
                ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();

                DataSet ds = bllBind.GetDataSet("SUM(InvoiceMoney) as SumInvoiceMoney,SUM(SumReceiveMoney) as SumReceiveMoneys", "V_FinaceSubInvoiceList", strWhere.ToString(), true);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    lblCount.Text = ChinaSoft.Common.Input.SetDecimal(ds.Tables[0].Rows[0]["SumInvoiceMoney"]).ToString();
                    lblPayCount.Text = ChinaSoft.Common.Input.SetDecimal(ds.Tables[0].Rows[0]["SumReceiveMoneys"]).ToString();
                    lblNotPayCount.Text = (Convert.ToDecimal(lblCount.Text) - Convert.ToDecimal(lblPayCount.Text)).ToString();
                }
                ChinaSoft.BLL.Common.Pager.BindGridView(gridView, AspNetPager1, "V_FinaceSubInvoiceList", "SubInvoiceId", "*", strWhere.ToString(), "CreateDate desc", "");
            }
            catch (Exception ex)
            {
                Response.Write("数据库错误：" + ex.Message);
                Response.End();
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Text = "<input type=\"checkbox\" id=\"cbSelectAll\" name=\"cbSelectAll\" onclick=\"javascript:checkJs(this.checked);\" />";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                //linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");

                //object obj1 = DataBinder.Eval(e.Row.DataItem, "Levels");
                //if ((obj1 != null) && ((obj1.ToString() != "")))
                //{
                //    e.Row.Cells[1].Text = obj1.ToString();
                //}
            }
        }

        #endregion

        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            BindData();
        }
    }
}